package DBLayer;
import ModelLayer.OrderDiscount;
import java.sql.*;
import java.util.ArrayList;

public class DBOrderDiscount {
    // <editor-fold defaultstate="collapsed" desc="Attributes & Constructors">
    private Connection conn;

    public DBOrderDiscount() {
        conn = SQLConnector.getInstance().getConnection();
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="SQL SELECT Methods">
    public OrderDiscount getOrderDiscountByID(int id) {
        return selectSingle("SELECT `id_order_discount`, `id_order`, `id_discount`, `name`, `value` FROM `ps_order_discount` WHERE `id_order_discount` = " + id + " LIMIT 0, 1;");
    }

    public ArrayList<OrderDiscount> getOrderDiscountList() {
        return selectMultiple("SELECT `id_order_discount`, `id_order`, `id_discount`, `name`, `value` FROM `ps_order_discount` ORDER BY `id_order_discount` ASC;");
    }

    private OrderDiscount selectSingle(String sqlStatement) {
        Statement stmt = null;
        ResultSet result = null;
        OrderDiscount orderDiscount = new OrderDiscount();

        try {
            stmt = conn.createStatement();
            result = stmt.executeQuery(sqlStatement);

            if (result.next()) {
                orderDiscount = buildOrderDiscount(result);
            }
            System.out.println("Fetching OrderDiscount Object");
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (result != null) {
                try {
                    result.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                result = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
        return orderDiscount;
    }

    private ArrayList<OrderDiscount> selectMultiple(String sqlStatement) {
        Statement stmt = null;
        ResultSet result = null;
        ArrayList<OrderDiscount> orderDiscounts = new ArrayList<OrderDiscount>();

        try {
            stmt = conn.createStatement();
            result = stmt.executeQuery(sqlStatement);

            while (result.next()) {
                OrderDiscount orderDiscount = new OrderDiscount();
                orderDiscount = buildOrderDiscount(result);
                orderDiscounts.add(orderDiscount);
            }
            System.out.println("Fetching OrderDiscount List");
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (result != null) {
                try {
                    result.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                result = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
        return orderDiscounts;
    }

    private OrderDiscount buildOrderDiscount(ResultSet result) {
        OrderDiscount orderDiscount = new OrderDiscount();
        try {
            orderDiscount.setIDOrderDiscount(result.getInt("id_order_discount"));
            orderDiscount.setIDOrder(result.getInt("id_order"));
            orderDiscount.setIDDiscount(result.getInt("id_discount"));
            orderDiscount.setName(result.getString("name"));
            orderDiscount.setValue(result.getFloat("value"));

        } catch (Exception e) {
            System.out.println("Exception: " + e.getMessage());
        }
        return orderDiscount;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="SQL INSERT, UPDATE & DELETE Methods">
    public int addOrderDiscount(OrderDiscount orderDiscount) {
        int returnID = 0;
        Statement stmt = null;
        ResultSet result = null;

        String query = "INSERT INTO `ps_order_discount` (`id_order_discount`, `id_order`, `id_discount`, `name`, `value`) "
                + "VALUES (" + orderDiscount.getIDOrderDiscount() + ", " + orderDiscount.getIDOrder() + ", " + orderDiscount.getIDDiscount() + ", '" + orderDiscount.getName() + "', " + orderDiscount.getValue() + ");";

        try {
            stmt = conn.createStatement();
            stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
            result = stmt.getGeneratedKeys();
            if (result.next()) {
                returnID = result.getInt(1);
            }
            System.out.println("Added OrderDiscount ID " + returnID);
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (result != null) {
                try {
                    result.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                result = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
        return returnID;
    }

    public int updateOrderDiscount(OrderDiscount orderDiscount, int id) {
        int returnID = 0;
        Statement stmt = null;
        String query = "UPDATE `ps_order_discount` SET "
                + "`id_order_discount`      = "  + orderDiscount.getIDOrderDiscount() + ", "
                + "`id_order`               = "  + orderDiscount.getIDOrder() + ", "
                + "`id_discount`            = "  + orderDiscount.getIDDiscount() + ", "
                + "`name`                   = '" + orderDiscount.getName() + "', "
                + "`value`                  = "  + orderDiscount.getValue() + " "
                + "WHERE `id_order` = " + id + " LIMIT 1;";

        try {
            stmt = conn.createStatement();
            stmt.executeUpdate(query);
            returnID = orderDiscount.getIDOrderDiscount();
            System.out.println("Updated OrderDiscount ID " + id);
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
        return returnID;
    }

    public void deleteOrderDiscount(int id) {
        Statement stmt = null;

        try {
            stmt = conn.createStatement();
            stmt.executeUpdate("DELETE FROM `ps_order_discount` WHERE `id_order_discount` = " + id + " LIMIT 1;");
            System.out.println("Deleted OrderDiscount ID " + id);
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
    }
    // </editor-fold>
}
